using System;
using Mbs.ComponentModel.Container.Metadata;

namespace Mbs.ComponentModel.Container.Listner
{
    [Serializable]
    public class KernelListnerManaer : ListnerManager<IKernelListner, LifestyleStage>, IKernelListnerManager, IKernelListner
    {
        public KernelListnerManaer() { }

        public IKernel Kernel { get; private set; }

        protected override void OnAfterRegister(IKernelListner listner)
        {
            listner.Init(Kernel);
        }

        public virtual void Init(IKernel kernel)
        {
            Kernel = kernel;
            if (kernel != null)
                Init();
        }

        protected virtual void Init() { }

        public LifestyleStage Type { get; private set; }

        void IKernelListner.OnPreCreate(IComponentInfo info)
        {
            Foreach(item => item.OnPreCreate(info));
        }

        void IKernelListner.OnCreated(IComponentInfo info, object instance)
        {
            Foreach(item => item.OnCreated(info, instance));
        }

        void IKernelListner.OnDestroyed(IComponentInfo info, object instance)
        {
            Foreach(item => item.OnDestroyed(info, instance));
        }
    }
}
